package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.religion.Bill;
import com.tongzhan.service.BillService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("/bill")
public class BillController {

    @Resource
    private BillService billService;

    /**
     * 新增财务接口
     *
     * @param userId 用户id
     * @param streetCode 街道编码
     * @param siteCode 场所编码
     * @param year 年度
     * @param month 月
     * @param content 内容
     * @param revenueData 收入类数据
     * @param outPutData 支出类数据
     * @param monthTotalRevenue 本月累计收入金额
     * @param monthTotalOutPut 本月累计支出金额
     * @return Return
     */
    @PostMapping("/newBill")
    public Return newBill(String billCode, String userId, String streetCode, String siteCode, String year, String month, String content,
                          String revenueData, String outPutData, BigDecimal monthTotalRevenue, BigDecimal monthTotalOutPut) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(streetCode) || StringUtils.isEmpty(siteCode) || StringUtils.isEmpty(year)
                || StringUtils.isEmpty(month) || StringUtils.isEmpty(revenueData) || StringUtils.isEmpty(outPutData)
                || monthTotalRevenue == null || monthTotalOutPut == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (StringUtils.isNotEmpty(content) && content.trim().length() > 5000) {
            return new Return(Return.FAIL, "内容过长");
        }
        Bill bill = new Bill();
        bill.setBillCode(billCode);
        bill.setSiteCode(siteCode);
        bill.setStreetCode(streetCode);
        bill.setYear(year);
        bill.setMonth(month);
        if (StringUtils.isNotEmpty(content)) {
            bill.setContent(content);
        }
        bill.setRevenueData(revenueData);
        bill.setOutPutData(outPutData);
        bill.setMonthTotalRevenue(monthTotalRevenue);
        bill.setMonthTotalOutPut(monthTotalOutPut);
        return billService.newBill(userId, bill);
    }

    /**
     * 获取财务列表接口
     *
     * @param userId 用户id
     * @param year 年度
     * @param month 月份
     * @param religionCode 宗教编码
     * @param siteCode 场所编码
     * @param state 状态
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/getBillList")
    public Return getBillList(String userId, String year, String religionCode, String siteCode, Integer state, String month,
                              Integer page, @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(userId) || state == null || page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return billService.getBillList(userId, year, religionCode, siteCode, state, page, size, month);
    }

    /**
     * 财务详情接口
     *
     * @param billCode 财务编码
     * @return Return
     */
    @PostMapping("/billDetails")
    public Return billDetails(String billCode) {
        if (StringUtils.isEmpty(billCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return billService.billDetails(billCode);
    }

    /**
     * 获取上月累计余额和本月累计余额接口
     *
     * @param userId 用户id
     * @return Return
     */
    @PostMapping("/getAmount")
    public Return getAmount(String userId) {
        if (StringUtils.isEmpty(userId)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return billService.getAmount(userId);
    }

    /**
     * 获取财务类型列表接口
     *
     * @param type 类型
     * @param religionCode 宗教编码
     * @return Return
     */
    @PostMapping("/getBillTypeList")
    public Return getBillTypeList(Integer type, String religionCode) {
        if (type == null || StringUtils.isEmpty(religionCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return billService.getBillTypeList(type, religionCode);
    }

    /**
     * 编辑财务类型接口
     *
     * @param billTypeCode 财务类型编码
     * @param billTypeName 财务类型名称
     * @param type 类型 0-收入类 1-支出类
     * @param religionCode 宗教编码
     * @return Return
     */
    @PostMapping("/editBillType")
    public Return editBillType(String billTypeCode, String billTypeName, Integer type, String religionCode) {
        if (StringUtils.isEmpty(billTypeName) || "".equals(billTypeName.trim()) || type == null || StringUtils.isEmpty(religionCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (billTypeName.trim().length() > 50) {
            return new Return(Return.FAIL, "类型名称过长");
        }
        return billService.editBillType(billTypeCode, billTypeName.trim(), type, religionCode);
    }

    /**
     * 删除财务接口
     *
     * @param billCode 财务编码
     * @return Return
     */
    @PostMapping("/delete")
    public Return delete(String billCode) {
        if (StringUtils.isEmpty(billCode)) {
            return new Return(Return.FAIL, "billCode 不能为空");
        }
        billService.delete(billCode);
        return new Return(Return.SUCCESS, "删除成功");
    }

    /**
     * 刪除财务类型接口
     *
     * @param billTypeCode 财务类型编码
     * @return Return
     */
    @PostMapping("/deleteBillType")
    public Return deleteBillType(String billTypeCode) {
        if (StringUtils.isEmpty(billTypeCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return billService.deleteBillType(billTypeCode);
    }
}
